package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.model.admin.bo.BaseParam1;
import com.cskaoyan.model.admin.vo.BaseRespVo;
import com.cskaoyan.model.vx.bo.WxCouponMyListBo;
import com.cskaoyan.model.vx.vo.WxCouponListVo;
import com.cskaoyan.model.vx.vo.WxCouponMyListVo;
import com.cskaoyan.model.vx.vo.WxCouponSelectListVo;
import com.cskaoyan.service.wx.WxCouponService;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.sun.prism.impl.BaseResourcePool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RequestMapping("wx/coupon")
@RestController
public class WxCouPonController {
    @Autowired
    WxCouponService wxCouponService;
    @Autowired
    HttpSession session;
    @GetMapping("mylist")
    public BaseRespVo mylist(WxCouponMyListBo wxCouponMyListBo){

        //假如获取Id为1

        Object userId = session.getAttribute("wxUserId");

        WxCouponMyListVo myListVo = wxCouponService.mylist(wxCouponMyListBo,(Integer) userId);
        return BaseRespVo.ok(myListVo);
    }
    @GetMapping("list")
    public BaseRespVo list(WxCouponMyListBo wxCouponMyListBo){
        WxCouponListVo wxCouponListVo = wxCouponService.list(wxCouponMyListBo);
        return BaseRespVo.ok(wxCouponListVo);
    }

    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody Map map,HttpSession session) {

        //Todo session获取userId
        Object userId = session.getAttribute("wxUserId");


        if(userId == null){
            return BaseRespVo.fail(501,"未登录");
        }

        Object couponId = map.get("couponId");
        Integer code = wxCouponService.receive((int) couponId, (Integer) userId);
        if(code == -1){
            return BaseRespVo.fail(740,"已经领取");
        }
        return BaseRespVo.ok(null);
    }

    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map){
        //通过post拿到请求兑换码,通过该条件拿到coupon表里的信息，
        // 然后插入到couponUser用户表里
        Object code = map.get("code");
        //通过session拿到userId
        Object userId = session.getAttribute("wxUserId");
        Integer row = wxCouponService.exchange((Integer) userId, (String) code);
        if(row != -1) return BaseRespVo.ok("null");
        return BaseRespVo.fail(740,"兑换失败");

    }

    @GetMapping("selectlist")
    public BaseRespVo selectlist(Integer cartId){
        //假说拿到userID
        Object userId = session.getAttribute("wxUserId");
        WxCouponSelectListVo seletlist = wxCouponService.seletlist(cartId, (Integer) userId);
        return BaseRespVo.ok(seletlist);
    }
}
